﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace WZ.WebSpider
{
    public class StockMarketSpider : IWebSpider<Entity.StockMarket>
    {
        public StockMarketSpider(string stockCode)
        {
            this.Code = stockCode;
        }
        public string Code { get; set; }
        public string Url
        {
            get { return string.Format("http://qt.gtimg.cn/q=sz{0}", this.Code); }
        }

        public Encoding ContextEncoding
        {
            get { return Encoding.GetEncoding("GBK"); }
        }

        public Regex regex
        {
            get { return new Regex(@"\~(?<SimplyName>[^~]*)\~(?<Code>[^~]*)\~(?<Price>[^~]*)\~(?<Yclose>[^~]*)\~(?<Topen>[^~]*)\~(?<Vol>[^~]*)\~(?<OutSize>[^~]*)\~(?<InSize>[^~]*)\~(?<Buy1>[^~]*)\~(?<TotalBuy1>[^~]*)\~(?<Buy2>[^~]*)\~(?<TotalBuy2>[^~]*)\~(?<Buy3>[^~]*)\~(?<TotalBuy3>[^~]*)\~(?<Buy4>[^~]*)\~(?<TotalBuy4>[^~]*)\~(?<Buy5>[^~]*)\~(?<TotalBuy5>[^~]*)\~(?<Sale1>[^~]*)\~(?<TotalSale1>[^~]*)\~(?<Sale2>[^~]*)\~(?<TotalSale2>[^~]*)\~(?<Sale3>[^~]*)\~(?<TotalSale3>[^~]*)\~(?<Sale4>[^~]*)\~(?<TotalSale4>[^~]*)\~(?<Sale5>[^~]*)\~(?<TotalSale5>[^~]*)\~(?<DealDetail>[^~]*)\~(?<GetTime>[^~]*)\~(?<UpDown>[^~]*)\~(?<UpDownPoint>[^~]*)\~(?<MaxPrice>[^~]*)\~(?<MinPrice>[^~]*)\~(?<SaleDetail>[^~]*)\~(?<VolHand>[^~]*)\~(?<TotalSalePrice>[^~]*)\~(?<TUN>[^~]*)\~(?<PE>[^~]*)\~(?<Other>[^~]*)\~(?<Max>[^~]*)\~(?<Min>[^~]*)\~(?<Amplitude>[^~]*)\~(?<CSV>[^~]*)\~(?<TMC>[^~]*)\~(?<PB>[^~]*)\~(?<HighLimit>[^~]*)\~(?<DropLimit>[^~]*)"); }
        }

        public List<Entity.StockMarket> GetWebData(string urlParam)
        {
            WZ.Utils.WebHttp.HttpClient client = new Utils.WebHttp.HttpClient(this.ContextEncoding);
            string postResult = client.GetString(this.Url);
            MatchCollection matchs = regex.Matches(postResult);
            List<Entity.StockMarket> entity = new List<Entity.StockMarket>();
            for (int i = 0; i < matchs.Count; i++)
            {
                entity.Add(WZ.WebSpider.Serialize.ConverModel.FillModel<Entity.StockMarket>(matchs[i].Groups));
            }
            return entity;
        }

        public bool Save(Entity.StockMarket t)
        {
            throw new NotImplementedException();
        }

        public int DepIndex
        {
            get { throw new NotImplementedException(); }
        }
    }
}
